package com.snack.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.snack.shop.entity.Address;
import com.snack.shop.mapper.AddressMapper;
import com.snack.shop.service.AddressService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 收货地址服务实现
 */
@Service
@RequiredArgsConstructor
public class AddressServiceImpl implements AddressService {

    private final AddressMapper addressMapper;

    @Override
    public List<Address> listByUserId(Long userId) {
        return addressMapper.selectList(
            new LambdaQueryWrapper<Address>()
                .eq(Address::getUserId, userId)
                .orderByDesc(Address::getIsDefault)
                .orderByDesc(Address::getCreateTime)
        );
    }

    @Override
    public Address getDetail(Long addressId) {
        return addressMapper.selectById(addressId);
    }

    @Override
    public Address getDefaultAddress(Long userId) {
        return addressMapper.selectOne(
            new LambdaQueryWrapper<Address>()
                .eq(Address::getUserId, userId)
                .eq(Address::getIsDefault, 1)
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Address address) {
        // 如果设置为默认地址，先取消其他默认地址
        if (address.getIsDefault() == 1) {
            addressMapper.update(null, 
                new LambdaUpdateWrapper<Address>()
                    .eq(Address::getUserId, address.getUserId())
                    .set(Address::getIsDefault, 0)
            );
        }
        
        addressMapper.insert(address);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Address address) {
        // 如果设置为默认地址，先取消其他默认地址
        if (address.getIsDefault() == 1) {
            addressMapper.update(null,
                new LambdaUpdateWrapper<Address>()
                    .eq(Address::getUserId, address.getUserId())
                    .ne(Address::getId, address.getId())
                    .set(Address::getIsDefault, 0)
            );
        }
        
        addressMapper.updateById(address);
    }

    @Override
    public void delete(Long addressId) {
        addressMapper.deleteById(addressId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setDefault(Long userId, Long addressId) {
        // 取消其他默认地址
        addressMapper.update(null,
            new LambdaUpdateWrapper<Address>()
                .eq(Address::getUserId, userId)
                .set(Address::getIsDefault, 0)
        );
        
        // 设置新的默认地址
        addressMapper.update(null,
            new LambdaUpdateWrapper<Address>()
                .eq(Address::getId, addressId)
                .set(Address::getIsDefault, 1)
        );
    }
}

